//
//  ShouYiViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/26.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "ShouYiViewController.h"
#import "BarLayerView.h"
#import "ZCBar.h"
#import "ShouYiDetailTableViewController.h"
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

@interface ShouYiViewController ()<detailViewPickerViewDelegate,BarLayerViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)CAShapeLayer *shapeLayer;

@property (nonatomic,strong)BarLayerView *barLayerView;

@property (nonatomic,weak)IBOutlet UIView *chartSuperView;
@property (nonatomic,weak)IBOutlet UIView *nianFenSuperView;
@property (nonatomic,weak)IBOutlet UIScrollView *chartScrollView;

@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,assign)NSInteger currentSelectedMonth;
@property(nonatomic,assign)NSInteger currentYear;
@property(nonatomic,assign)NSInteger currentSelectedYear;
@property(nonatomic,assign)CGFloat maxValue;

@property (weak, nonatomic) IBOutlet UILabel *yueFenLabel;
@property (weak, nonatomic) IBOutlet UILabel *zongJiLabel;
@property (weak, nonatomic) IBOutlet UILabel *jingShouRuLabel;
@property (weak, nonatomic) IBOutlet UILabel *zuJinLabel;
@property (weak, nonatomic) IBOutlet UILabel *guanLiFeiLabel;
@property (weak, nonatomic) IBOutlet UILabel *xiuShanFeiLabel;
@property (weak, nonatomic) IBOutlet UILabel *riChangZaFeiLabel;
@property (weak, nonatomic) IBOutlet UILabel *daiGuanFeiLabel;
@property (weak, nonatomic) IBOutlet UILabel *sheBeiWeiXiuFeiLabel;
@property (weak, nonatomic) IBOutlet UILabel *shuiJinLabel;
@property (weak, nonatomic) IBOutlet UILabel *shouXvFeiLabel;
@property (weak, nonatomic) IBOutlet UILabel *qiTaFeiYongLabel;

@property (weak, nonatomic) IBOutlet UILabel *totalJMoneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalMoneyLabel;

@property(nonatomic,weak)IBOutlet UILabel *nianFenLabel;
@end

@implementation ShouYiViewController

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 10;
}

- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 1;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *year = [NSString stringWithFormat:@"%@年",@(self.currentYear-row)];
    NSLog(@"%@",year);
    return year;
}

-(void)detailViewPickerViewDidClose{
    NSInteger index = [self.fatherViewController.pickerView selectedRowInComponent:0];
    self.currentSelectedYear = self.currentYear-index;
    [self.nianFenLabel setText:[NSString stringWithFormat:@"%@年",@(self.currentSelectedYear)]];
    [self sendRequestForLoadData];
}

-(IBAction)showPickerView{
    self.fatherViewController.pickerView.dataSource = self;
    self.fatherViewController.pickerView.delegate = self;
    self.fatherViewController.delegate = self;
    [self.fatherViewController.pickerView reloadAllComponents];
    [self.fatherViewController showPickerView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentSelectedMonth=-1;
    [self.nianFenSuperView.layer setBorderColor:[UIColor colorWithRed:132.0f/255.0f green:138.0f/255.0f blue:152.0f/255.0f alpha:1.0f].CGColor];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    self.currentSelectedYear = self.currentYear = comp.year;
    [self.nianFenLabel setText:[NSString stringWithFormat:@"%@年",@(self.currentSelectedYear)]];
    [self sendRequestForLoadData];
}
-(void)sendRequestForLoadData{
    [self resetView];
    NSString *urlString =nil;
    if (self.fatherViewController.isGongYu) {
        urlString= [NSString stringWithFormat:@"%@/realty/room-earning.do?roomId=%@&accountDate=%@&moneyTypeCode=%@",JSXF_HOST,self.roomId,@(self.currentSelectedYear),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"]];
    }else{
        urlString= [NSString stringWithFormat:@"%@/realty/room-earning.do?realtyId=%@&accountDate=%@&moneyTypeCode=%@",JSXF_HOST,self.fatherViewController.realtyId,@(self.currentSelectedYear),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"]];
    }
    
    NSLog(@"%@",urlString);
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = response;
        if (![response[@"data"] isKindOfClass:[NSString class]]) {
            self.dataArray = dic[@"data"][@"everyMonth"];
            self.totalJMoneyLabel.text = [NSString stringWithFormat:@"%@",FORMAT_PRICE(dic[@"data"][@"totalJMoney"])];
            self.totalMoneyLabel.text = [NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"data"][@"totalMoney"])];
            [self dealData];
            [self setupChartView];
        }
        
        //        self.dataDic = dic[@"data"];
        //        [self resetView];
    }];
}

-(void)dealData{
//    CGFloat sum = 0.0f;
    self.maxValue = 0.0f;
    for (NSDictionary *dic in self.dataArray) {
        NSNumber *zzc = dic[@"Jzzc"];
        NSNumber *jsr = dic[@"Jjsr"];
        CGFloat result = fabsf(zzc.floatValue) + fabsf(jsr.floatValue);
        if (self.maxValue < result) {
            self.maxValue = result;
        }
    }
//    self.zongJiJinELabel.text = [NSString stringWithFormat:@"%@",@(sum)];
}

-(void)resetView{
    self.yueFenLabel.text = @"-月费用详情";
    self.zongJiLabel.text = @" ";
    self.zongJiLabel.attributedText = nil;
    self.jingShouRuLabel.text = @" ";
    self.jingShouRuLabel.attributedText = nil;
    self.zuJinLabel.text = @" ";
    self.zuJinLabel.attributedText = nil;
    self.guanLiFeiLabel.text = @" ";
    self.guanLiFeiLabel.attributedText = nil;
    self.xiuShanFeiLabel.text = @" ";
    self.xiuShanFeiLabel.attributedText = nil;
    self.riChangZaFeiLabel.text = @" ";
    self.riChangZaFeiLabel.attributedText = nil;
    self.daiGuanFeiLabel.text = @" ";
    self.daiGuanFeiLabel.attributedText = nil;
    self.sheBeiWeiXiuFeiLabel.text = @" ";
    self.sheBeiWeiXiuFeiLabel.attributedText = nil;
    self.shuiJinLabel.text = @" ";
    self.shuiJinLabel.attributedText = nil;
    self.shouXvFeiLabel.text = @" ";
    self.shouXvFeiLabel.attributedText = nil;
    self.qiTaFeiYongLabel.text = @" ";
    self.qiTaFeiYongLabel.attributedText = nil;
    
    
}

-(void)barLayerView:(BarLayerView *)barLayerView didSelectedOfIndex:(NSInteger)index{
    self.currentSelectedMonth=index;
    NSDictionary *dic = self.dataArray[index];
    NSLog(@"didClick:%@",dic);
    NSString *yueFenString = [((NSString *)dic[@"accountDate"])componentsSeparatedByString:@"-"].lastObject;
    self.yueFenLabel.text = [NSString stringWithFormat:@"%@月费用详情",yueFenString];
    self.zongJiLabel.text = [NSString stringWithFormat:@"%@\n(%@%@)",FORMAT_PRICE(dic[@"Jzzc"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"zzc"])];
    NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:self.zongJiLabel.text attributes:@{NSFontAttributeName: self.zongJiLabel.font,NSForegroundColorAttributeName: self.zongJiLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.zongJiLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"zzc"])]]];
    self.zongJiLabel.attributedText = string;
    
    self.jingShouRuLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"Jjsr"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"jsr"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.jingShouRuLabel.text attributes:@{NSFontAttributeName: self.jingShouRuLabel.font,NSForegroundColorAttributeName: self.jingShouRuLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.jingShouRuLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"jsr"])]]];
    self.jingShouRuLabel.attributedText = string;
    
    self.zuJinLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"Jrent"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"rent"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.zuJinLabel.text attributes:@{NSFontAttributeName: self.zuJinLabel.font,NSForegroundColorAttributeName: self.zuJinLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.zuJinLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"rent"])]]];
    self.zuJinLabel.attributedText = string;
    
    self.guanLiFeiLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JmanageCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"manageCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.guanLiFeiLabel.text attributes:@{NSFontAttributeName: self.guanLiFeiLabel.font,NSForegroundColorAttributeName: self.guanLiFeiLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.guanLiFeiLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"manageCost"])]]];
    self.guanLiFeiLabel.attributedText = string;
    
    self.xiuShanFeiLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JfixCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"fixCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.xiuShanFeiLabel.text attributes:@{NSFontAttributeName: self.xiuShanFeiLabel.font,NSForegroundColorAttributeName: self.xiuShanFeiLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.xiuShanFeiLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"fixCost"])]]];
    self.xiuShanFeiLabel.attributedText = string;
    
    self.riChangZaFeiLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JdayCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"dayCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.riChangZaFeiLabel.text attributes:@{NSFontAttributeName: self.riChangZaFeiLabel.font,NSForegroundColorAttributeName: self.riChangZaFeiLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.riChangZaFeiLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"dayCost"])]]];
    self.riChangZaFeiLabel.attributedText = string;
    
    self.daiGuanFeiLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JagencyCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"agencyCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.daiGuanFeiLabel.text attributes:@{NSFontAttributeName: self.daiGuanFeiLabel.font,NSForegroundColorAttributeName: self.daiGuanFeiLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.daiGuanFeiLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"agencyCost"])]]];
    self.daiGuanFeiLabel.attributedText = string;
    
    self.sheBeiWeiXiuFeiLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JeqFixCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"eqFixCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.sheBeiWeiXiuFeiLabel.text attributes:@{NSFontAttributeName: self.sheBeiWeiXiuFeiLabel.font,NSForegroundColorAttributeName: self.sheBeiWeiXiuFeiLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.sheBeiWeiXiuFeiLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"eqFixCost"])]]];
    self.sheBeiWeiXiuFeiLabel.attributedText = string;
    
    self.shuiJinLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JtaxesCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"taxesCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.shuiJinLabel.text attributes:@{NSFontAttributeName: self.shuiJinLabel.font,NSForegroundColorAttributeName: self.shuiJinLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.shuiJinLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"taxesCost"])]]];
    self.shuiJinLabel.attributedText = string;
    
    self.shouXvFeiLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JserviceCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"serviceCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.shouXvFeiLabel.text attributes:@{NSFontAttributeName: self.shouXvFeiLabel.font,NSForegroundColorAttributeName: self.shouXvFeiLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.shouXvFeiLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"serviceCost"])]]];
    self.shouXvFeiLabel.attributedText = string;
    
    self.qiTaFeiYongLabel.text = [NSString stringWithFormat:@"%@(%@%@)",FORMAT_PRICE(dic[@"JotherCost"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"otherCost"])];
    string = [[NSMutableAttributedString alloc] initWithString:self.qiTaFeiYongLabel.text attributes:@{NSFontAttributeName: self.qiTaFeiYongLabel.font,NSForegroundColorAttributeName: self.qiTaFeiYongLabel.textColor}];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[self.qiTaFeiYongLabel.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"otherCost"])]]];
    self.qiTaFeiYongLabel.attributedText = string;
}

-(void)setupChartView{
    //测试数据
    //单个柱子上的分段数据
    NSMutableArray *dataArr = [[NSMutableArray alloc]initWithCapacity:0];
    NSMutableArray *xArr = [[NSMutableArray alloc]initWithCapacity:0];
    
    for (NSDictionary *dic in self.dataArray) {
        NSNumber *zzc = dic[@"Jjsr"];
        NSNumber *jsr = dic[@"Jzzc"];
        
        NSString *yueFenString = [((NSString *)dic[@"accountDate"])componentsSeparatedByString:@"-"].lastObject;
        [xArr addObject:[NSString stringWithFormat:@"%@月",yueFenString]];
        
        [dataArr addObject:@[@{@"precent":@(zzc.floatValue / self.maxValue * MAX_TIME),@"strokeColor":@"1"},@{@"precent":@(fabsf(jsr.floatValue) / self.maxValue * MAX_TIME),@"strokeColor":jsr.integerValue>0?@"2":@"3"}]];
    }
    
    //    self.barLayerView = [[BarLayerView alloc]initWithFrame:CGRectMake(00, 00, self.view.bounds.size.width, self.chartSuperView.frame.size.height)];
    if (self.barLayerView) {
        [self.barLayerView removeFromSuperview];
        self.barLayerView=nil;
    }
    self.barLayerView = [[BarLayerView alloc]initWithFrame:CGRectMake(00, 00, xArr.count * (CHART_BAR_INTER+CHART_BAR_WIDTH), self.chartSuperView.frame.size.height)];
    self.barLayerView.delegate = self;
    self.barLayerView.backgroundColor = [UIColor clearColor];
    [self.barLayerView setDataArr:dataArr];//设置柱子进度
    
    [self.barLayerView setXArr:xArr];//设置XLable
    
    [self.barLayerView strokeMKBarChart];//重绘
    
    //    [self.chartSuperView addSubview:self.barLayerView];
    [self.chartScrollView addSubview:self.barLayerView];
    [self.chartScrollView setContentSize:self.barLayerView.frame.size];
    
    if (self.dataArray.count>0) {
        [self barLayerView:self.barLayerView didSelectedOfIndex:0];
    }
}


 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
     if ([segue.destinationViewController isKindOfClass:[ShouYiDetailTableViewController class]]) {
         ShouYiDetailTableViewController *vc = segue.destinationViewController;
         NSDictionary *dic = self.dataArray[self.currentSelectedMonth];
         vc.dataArray = dic[@"rooms"];
     }
 }




@end
